import pymysql
from Config.Config import *

# Aide3.0-open_ad-day数据库连接
class DBAdMiddlePlatform:
    def __init__(self):
        self.conn = pymysql.connect(host=db_host_device_middle,
                                    port=db_port_device_middle,
                                    user=db_user_device_middle,
                                    passwd=db_passwd_device_middle,  # passwd 不是 password
                                    db=db_ad_middle)
        self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)  # 查以字典格式返回查询结果

    def selectDB(self, sql):
        """查询数据"""
        # self.cursor = self.db.cursor()  # 以元组格式返回查询结果
        #self.cursor = self.db.cursor(cursor=pymysql.cursors.DictCursor)  # 查以字典格式返回查询结果
        try:
            self.cur.execute(sql)
            data = self.cur.fetchall()
        except Exception as err:
            data = tuple()
        finally:
            self.cur.close()
        return data

    def deleteDB(self, sql):
        """删除数据"""
        #self.cursor = self.db.cursor(cursor=pymysql.cursors.DictCursor)  # 查以字典格式返回查询结果
        try:
            self.cur.execute(sql)  # 执行sql
            self.conn.commit()  # 提交更改
        except Exception as err:
            self.conn.rollback()  # 回滚
        finally:
            self.cur.close()

    def closeDB(self):
        """关闭数据库连接"""
        self.conn.close()

# Aide3.0-open_order-day数据库连接
class DBOrderMiddlePlatform:
    def __init__(self):
        self.conn = pymysql.connect(host=db_host_device_middle,
                                    port=db_port_device_middle,
                                    user=db_user_device_middle,
                                    passwd=db_passwd_device_middle,  # passwd 不是 password
                                    db=db_order_middle)
        self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)  # 查以字典格式返回查询结果

    def selectDB(self, sql):
        """查询数据"""
        try:
            self.cur.execute(sql)
            data = self.cur.fetchall()
        except Exception as err:
            data = tuple()
        finally:
            self.cur.close()
        return data

    def closeDB(self):
        """关闭数据库连接"""
        self.conn.close()

# Aide3.0-open_user-day数据库连接
class DBUserMiddlePlatform:
    def __init__(self):
        self.conn = pymysql.connect(host=db_host_device_middle,
                                    port=db_port_device_middle,
                                    user=db_user_device_middle,
                                    passwd=db_passwd_device_middle,  # passwd 不是 password
                                    db=db_user_middle)
        self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)  # 查以字典格式返回查询结果

    def selectDB(self, sql):
        """查询数据"""
        try:
            self.cur.execute(sql)
            data = self.cur.fetchall()
        except Exception as err:
            data = tuple()
        finally:
            self.cur.close()
        return data

    def closeDB(self):
        """关闭数据库连接"""
        self.conn.close()









